CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to convert String to Char[] and Char[] to String in C# ?
Submitted By Satheesh Babu B
On 6/28/2010 8:04:01 AM
Tags: C#,CodeDigest  

How to convert String to Char[] and Char[] to String in C# ?

 

This is a little tip which helps us to convert a C# char array to string and a string variable to char array.

 

1. We can convert a string variable to char array using a method called ToCharArray() in string class. Refer below,


string str = "I Love ASP.Net!";
        char[] ch = str.ToCharArray();
        for (int i = 0; i < ch.Length; i++)
        {
            Response.Write(ch[i] + "<BR>");
        }

 

2. We can convert a character array back to string variable by using string class constructor. See below,


string strtarget = new string(ch);
       Response.Write(strtarget);


In the above code, ch is a type of char[] .

Happy Coding!!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..